feat(payments): Add account id build method#73
Conversation
WalkthroughA new utility class Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In
`@src/main/java/com/formance/formance_sdk/utils/custom_helpers/PaymentsAccountId.java`:
- Around line 25-26: The JSON building in PaymentsAccountId currently only
escapes backslashes and quotes for `reference`, which misses control characters
and can produce invalid JSON; add a private helper method (e.g.,
escapeJson(String s) in the PaymentsAccountId class) that escapes backslash and
quote and converts control characters (\b, \f, \n, \r, \t) and any codepoint <
0x20 into their proper JSON escape sequences (use \u00XX for other control
chars), then replace the direct replace(...) call and pass
`escapeJson(reference)` into the String.format call that builds `json` (the code
referencing `connectorIdJson` and `reference`).
| String connectorIdJson = new String(Base64.getDecoder().decode(connectorId), StandardCharsets.UTF_8); | ||
| String json = String.format("{\"ConnectorID\":%s,\"Reference\":\"%s\"}", connectorIdJson, reference.replace("\\", "\\\\").replace("\"", "\\\"")); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# First, locate and examine the PaymentsAccountId.java file
fd "PaymentsAccountId.java" --type fRepository: formancehq/formance-sdk-java
Length of output: 155
🏁 Script executed:
# Check if the file exists and read its content
cat -n "src/main/java/com/formance/formance_sdk/utils/custom_helpers/PaymentsAccountId.java"Repository: formancehq/formance-sdk-java
Length of output: 1948
🏁 Script executed:
# Search for JSON-related imports and utilities in the codebase
rg "import.*json|import.*JSON" --type java | head -20Repository: formancehq/formance-sdk-java
Length of output: 367
🏁 Script executed:
# Check if there are existing JSON escaping patterns in the codebase
rg "escapeJson|JsonEscape|replace.*\\\\\\\\|StandardCharsets" --type java -B 2 -A 2 | head -40Repository: formancehq/formance-sdk-java
Length of output: 4084
Escape JSON control characters in reference to prevent malformed IDs.
The current escaping only handles backslash and quote. Control characters like newline, tab, and other characters below 0x20 are not escaped, resulting in invalid JSON when such characters appear in reference. This breaks the account ID format expected by the Formance API.
Add a helper method to properly escape all JSON-required characters:
Suggested fix (local escape helper)
- String json = String.format("{\"ConnectorID\":%s,\"Reference\":\"%s\"}", connectorIdJson, reference.replace("\\", "\\\\").replace("\"", "\\\""));
+ String json = String.format("{\"ConnectorID\":%s,\"Reference\":\"%s\"}", connectorIdJson, escapeJsonString(reference));
@@
public static String Build(String connectorId, String reference) {
@@
}
+
+ private static String escapeJsonString(String input) {
+ StringBuilder sb = new StringBuilder(input.length() + 16);
+ for (int i = 0; i < input.length(); i++) {
+ char c = input.charAt(i);
+ switch (c) {
+ case '\\': sb.append("\\\\"); break;
+ case '"': sb.append("\\\""); break;
+ case '\b': sb.append("\\b"); break;
+ case '\f': sb.append("\\f"); break;
+ case '\n': sb.append("\\n"); break;
+ case '\r': sb.append("\\r"); break;
+ case '\t': sb.append("\\t"); break;
+ default:
+ if (c < 0x20) {
+ sb.append(String.format("\\u%04x", (int) c));
+ } else {
+ sb.append(c);
+ }
+ }
+ }
+ return sb.toString();
+ }🤖 Prompt for AI Agents
In
`@src/main/java/com/formance/formance_sdk/utils/custom_helpers/PaymentsAccountId.java`
around lines 25 - 26, The JSON building in PaymentsAccountId currently only
escapes backslashes and quotes for `reference`, which misses control characters
and can produce invalid JSON; add a private helper method (e.g.,
escapeJson(String s) in the PaymentsAccountId class) that escapes backslash and
quote and converts control characters (\b, \f, \n, \r, \t) and any codepoint <
0x20 into their proper JSON escape sequences (use \u00XX for other control
chars), then replace the direct replace(...) call and pass
`escapeJson(reference)` into the String.format call that builds `json` (the code
referencing `connectorIdJson` and `reference`).
Fixes EN-708